<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="农事活动" prop="husbandryEvents">
        <el-select v-model="queryParams.husbandryEvents" placeholder="请选择农事活动" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PRODUCE_HUSBANDRY_EVENTS)" :key="dict.value"
                     :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="生产信息id" prop="infoId">
        <el-input v-model="queryParams.infoId" placeholder="请输入生产信息id" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="计划日期" prop="planTime">
        <el-date-picker v-model="queryParams.planTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="执行状态" prop="executeStatus">
        <el-select v-model="queryParams.executeStatus" placeholder="请选择执行状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PRODUCE_HUSBANDRY_EXECUTE_STATUS)" :key="dict.value"
                     :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="执行时间" prop="executeTime">
        <el-date-picker v-model="queryParams.executeTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="耗时（时）" prop="take">
        <el-input v-model="queryParams.take" placeholder="请输入耗时（时）" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="农事图片" prop="husbandryImg">
        <el-input v-model="queryParams.husbandryImg" placeholder="请输入农事图片" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="创建日期" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['produce:husbandry:create']">新增
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"-->
<!--                   :loading="exportLoading" v-hasPermi="['produce:husbandry:export']">导出-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
<!--      <el-table-column label="主键" align="center" prop="id"/>-->
      <el-table-column label="农事活动" align="center" prop="husbandryEvents">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.PRODUCE_HUSBANDRY_EVENTS" :value="scope.row.husbandryEvents"/>
        </template>
      </el-table-column>
      <el-table-column label="生产信息id" align="center" prop="infoId"/>
      <el-table-column label="计划日期" align="center" prop="planTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.planTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行状态" align="center" prop="executeStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.PRODUCE_HUSBANDRY_EXECUTE_STATUS" :value="scope.row.executeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="执行人id" align="center" prop="memberId"/>
      <el-table-column label="执行时间" align="center" prop="executeTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.executeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="耗时（时）" align="center" prop="take"/>
<!--      <el-table-column label="农事图片" align="center" prop="husbandryImg"/>-->
      <el-table-column label="创建日期" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['produce:husbandry:update']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['produce:husbandry:delete']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="1300px" v-dialogDrag append-to-body class="addDialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="农事活动" prop="husbandryEvents">
          <el-select v-model="form.husbandryEvents" placeholder="请选择农事活动">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PRODUCE_HUSBANDRY_EVENTS)" :key="dict.value"
                       :label="dict.label" :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="生产信息id" prop="infoId">
          <el-input v-model="form.infoId" placeholder="请输入生产信息id"/>
        </el-form-item>
        <el-form-item label="计划日期" prop="planTime">
          <el-date-picker clearable v-model="form.planTime" type="date" value-format="timestamp" placeholder="选择计划日期"
                          style="width:100%"/>
        </el-form-item>
        <el-form-item label="执行状态" prop="executeStatus">
          <el-select v-model="form.executeStatus" placeholder="请选择执行状态" style="width:100%">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PRODUCE_HUSBANDRY_EXECUTE_STATUS)" :key="dict.value"
                       :label="dict.label" :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="执行人" prop="memberId">
          <el-cascader :options="memberOptions" v-model="memberId" :props="{ expandTrigger: 'hover' }"
                       placeholder="请选择员工" style="width: 250px">
          </el-cascader>
        </el-form-item>
        <el-form-item label="执行时间" prop="executeTime">
          <el-date-picker clearable v-model="form.executeTime" type="date" value-format="timestamp" placeholder="选择执行时间"
                          style="width:100%"/>
        </el-form-item>
        <el-form-item label="耗时（时）" prop="take">
          <el-input v-model="form.take" placeholder="请输入耗时（时）"/>
        </el-form-item>
        <el-form-item label="农事图片" prop="husbandryImg">
          <ImageUpload v-model="form.husbandryImg" />
        </el-form-item>
        <el-table :data="inputData" border>
          <el-table-column prop="inputName" label="投入品名称" align="center">
            <template v-slot="scope">
              <el-input v-model="scope.row.inputName" placeholder="请输入投入品名称"/>
            </template>
          </el-table-column>
          <el-table-column prop="inputCount" label="投入品数量" align="center">
            <template v-slot="scope">
              <el-input-number v-model="scope.row.inputCount" label="投入品数量" style="width: 100%"/>
            </template>
          </el-table-column>
          <el-table-column prop="inputType" label="投入品类别" align="center">
            <template v-slot="scope">
              <el-select v-model="scope.row.inputType" placeholder="请选择投入品类别" style="width: 100%">
                <el-option v-for="dict in getDictDatas(
                      DICT_TYPE.AGRO_INPUT_TYPE
                    )" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="inputDescribe" label="投入品描述" align="center">
            <template v-slot="scope">
              <el-input v-model="scope.row.inputDescribe" placeholder="请输入投入品描述"/>
            </template>
          </el-table-column>
          <el-table-column prop="inputPrice" label="投入品价格" align="center">
            <template v-slot="scope">
              <el-input v-model="scope.row.inputPrice" placeholder="请输入投入品价格"/>
            </template>
          </el-table-column>
          <el-table-column prop="inputTime" label="投入时间" align="center">
            <template v-slot="scope">
              <el-date-picker v-model="scope.row.inputTime" value-format="time  stamp" type="date"
                              style="width: 100%"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <template v-slot="scope">
              <el-button size="mini" type="text" icon="el-icon-plus" @click="addInput(scope.row)"></el-button>
              <el-button size="mini" type="text" icon="el-icon-minus" @click="reduceInput(scope.$index, scope.row)">
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createHusbandry,
  deleteHusbandry,
  exportHusbandryExcel,
  getHusbandry,
  getHusbandryPage,
  updateHusbandry
} from "@/api/produce/husbandry";
import ImageUpload from '@/components/ImageUpload';
import {listAllMember} from "@/api/member/memberUser";

export default {
  name: "Husbandry",
  components: {
    ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产管理农事列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        husbandryEvents: null,
        infoId: null,
        planTime: [],
        executeStatus: null,
        memberId: null,
        executeTime: [],
        take: null,
        husbandryImg: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      memberId: null,
      memberOptions: null,
      inputData: [{}]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getHusbandryPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
      listAllMember().then((resp) => {
        this.memberOptions = resp.data.map(user => {
          return {value: '' + user.id, label: user.nickname}
        })
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        husbandryEvents: undefined,
        infoId: undefined,
        planTime: undefined,
        executeStatus: undefined,
        memberId: undefined,
        executeTime: undefined,
        take: undefined,
        husbandryImg: undefined,
      };
      this.memberId = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产管理农事";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getHusbandry(id).then(response => {
        this.form = response.data;
        this.memberId = response.data.memberId + "";
        if (this.form.inputUpdateReqVOS != null && this.form.inputUpdateReqVOS.length > 0) {
          this.inputData = this.form.inputUpdateReqVOS;
        }
        this.open = true;
        this.title = "修改生产管理农事";
      });

    },
    /** 移除投入品 */
    reduceInput(index) {
      if (this.inputData.length > 1) {
        this.inputData.splice(index, 1);
      } else {
        this.inputData = [{}];
      }

    },
    /** 添加投入品 */
    addInput() {
      this.inputData.push({});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        if (this.memberId != null) {
          this.form.memberId = this.memberId[this.memberId.length - 1];
        }
        // 修改的提交
        if (this.form.id != null) {
          this.form.inputUpdateReqVOS = this.inputData;
          updateHusbandry(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        this.form.inputCreateReqVOS = this.inputData;
        createHusbandry(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除生产管理农事编号为"' + id + '"的数据项?').then(function () {
        return deleteHusbandry(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有生产管理农事数据项?').then(() => {
        this.exportLoading = true;
        return exportHusbandryExcel(params);
      }).then(response => {
        this.$download.excel(response, '生产管理农事.xls');
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
